# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=cups
pkgver=2.4.7
pkgrel=4
pkgdesc="The CUPS Printing System"
url="https://github.com/OpenPrinting/cups/"
arch="all"
license="Apache-2.0"
depends_dev="libgcrypt-dev gnutls-dev zlib-dev"
makedepends="
	$depends_dev
	avahi-dev
	dbus-dev
	libjpeg-turbo-dev
	libpaper-dev
	libusb-dev
	linux-headers
	"
depends="cups-client poppler-utils openssl dbus"
install="cups.pre-install"
pkggroups="lp lpadmin"
pkgusers="lp"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-doc
	$pkgname-libs
	ipptool
	$pkgname-client
	$pkgname-lang
	$pkgname-openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/OpenPrinting/cups/archive/v$pkgver.tar.gz
	$pkgname.logrotate
	cupsd.initd
	cups-no-export-ssllibs.patch
	cups-nostrip.patch
	default-config-no-gssapi.patch
	"
options="!check"

# secfixes:
#   2.4.7-r0:
#     - CVE-2023-4504
#   2.4.2-r7:
#     - CVE-2023-32324
#   2.4.2-r0:
#     - CVE-2022-26691
#   2.3.3-r0:
#     - CVE-2020-3898
#     - CVE-2019-8842
#   2.2.12-r0:
#     - CVE-2019-8696
#     - CVE-2019-8675
#   2.2.10-r0:
#     - CVE-2018-4700

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--libdir=/usr/lib \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-menudir=/usr/share/applications \
		--with-icondir=/usr/share/icons \
		--with-logdir=/var/log/cups \
		--with-docdir=/usr/share/cups \
		--with-rundir=/run/cups \
		--with-cupsd-file-perm=0755 \
		--with-cups-user=lp \
		--with-cups-group=lp \
		--with-system-groups=lpadmin \
		--with-domainsocket=/run/cups/cups.sock \
		--enable-libusb \
		--without-rcdir \
		--without-php \
		--disable-pam \
		--enable-raw-printing \
		--enable-dbus \
		--with-dbusdir=/usr/share/dbus-1 \
		--enable-libpaper \
		--enable-ssl=yes \
		--enable-gnutls \
		--disable-launchd \
		--with-optim="$CFLAGS -flto=auto"
	make
}

package() {
	make BUILDROOT="$pkgdir" install

	cd "$pkgdir"

	# These no longer works since CUPS >= 1.6 (http://www.cups.org/str.php?L4120).
	rm -rf usr/share/cups/banners/* \
		usr/share/cups/data/testprint

	install -D -m 644 "$srcdir"/cups.logrotate etc/logrotate.d/cups
	install -D -m 755 "$srcdir"/cupsd.initd etc/init.d/cupsd

	if [ -e usr/share/applications/cups.desktop ] ; then
		sed -i 's|^Exec=htmlview http://localhost:631/|Exec=xdg-open http://localhost:631/|g' \
			usr/share/applications/cups.desktop
	fi
	find usr/share/cups/model -name "*.ppd" -exec gzip -n9f {} +
}

dev() {
	default_dev

	# needed for main cups operation
	mv "$subpkgdir"/usr/share/cups/ppdc/* \
		"$pkgdir"/usr/share/cups/ppdc
	rmdir "$subpkgdir"/usr/share/cups/ppdc
}

libs() {
	pkgdesc="CUPS libraries"
	depends=""
	replaces="libcups"

	amove 'usr/lib/*.so*'
	install -d "$pkgdir"/etc/cups
}

ipptool() {
	pkgdesc="Perform internet printing protocol requests"
	depends=""

	amove usr/bin/ipptool \
		usr/share/cups/ipptool
}

client() {
	pkgdesc="CUPS command-line client programs"
	depends=""

	amove usr/bin \
		usr/sbin/cupsaccept \
		usr/sbin/cupsctl \
		usr/sbin/cupsdisable \
		usr/sbin/cupsenable \
		usr/sbin/lpadmin \
		usr/sbin/lpc \
		usr/sbin/lpinfo \
		usr/sbin/lpmove
}

doc() {
	default_doc

	amove usr/share/cups/help
}

sha512sums="
27ca505a2868aa7bc248bac892aafe2a837633e73b6059d3ab4812264e3b0e786ef075751e8cc4300ce6bc43ef095e3d77dd3fce88ce8e72ca69b65093427bca  cups-2.4.7.tar.gz
cf64211da59e79285f99d437c02fdd7db462855fb2920ec9563ba47bd8a9e5cbd10555094940ceedeb41ac805c4f0ddb9147481470112a11a76220d0298aef79  cups.logrotate
2c2683f755a220166b3a1653fdd1a6daa9718c8f0bbdff2e2d5e61d1133306260d63a83d3ff41619b5cf84c4913fae5822b79553e2822858f38fa3613f4c7082  cupsd.initd
148eb01f0861e1cd0861a174a5c5262c49b079b27b972b4e90656b9b9357ec710e861604c30e897818f550e9c1cdcefcd8c8bc955ba4a333902a74cdd8dfb5a6  cups-no-export-ssllibs.patch
7b2f1212b009d4e005eaf4fc288413bde57aff4ca664173202163e8f18be1ddc72c26ec84576a4850d63b08c6ce5948d57c77e81c4f4c85032d7ef7530b1eb81  cups-nostrip.patch
c492f5e20a1bb73cf6bed86d0343659e839df4003aa1409bf608dd3ff05694d1e06ba26fd3786385d2c96dd668485dcef6cfee7f908416a2b125a8e30e82e670  default-config-no-gssapi.patch
"
